package com.cqtczh.qhd.driver.acticity;

import httptools.HttpJsonTool;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tools.AppManager;
import tools.L;
import tools.LogFileFind;
import tools.SD_Finder;

import cn.jpush.android.api.JPushInterface;

import com.baidu.android.pushservice.CustomPushNotificationBuilder;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.baidu.location.LocationClient;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.slidingmenu.lib.SlidingMenu;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.AppContent.CompleteCarTeam;
import com.cqtczh.qhd.driver.AppContent.GetCarTeamDelete;
import com.cqtczh.qhd.driver.AppContent.getCarTeamMessage;
import com.cqtczh.qhd.driver.AppContent.menuCEHD;
import com.cqtczh.qhd.driver.AppContent.userInfoCheck;
import com.cqtczh.qhd.driver.acticity.carteam.CarTeamConter;
import com.cqtczh.qhd.driver.acticity.carteam.CarTeamRequestInfo;
import com.cqtczh.qhd.driver.acticity.carteam.Car_Team_Info;
import com.cqtczh.qhd.driver.acticity.order.OrdersActivity;
import com.cqtczh.qhd.driver.acticity.user.MessageActivity;
import com.cqtczh.qhd.driver.acticity.user.UserLoginActivity;
import com.cqtczh.qhd.driver.api.GagApi;
import com.cqtczh.qhd.driver.model.DownLoadAPP;
import com.cqtczh.qhd.driver.utils.SPUtils;
import com.cqtczh.qhd.driver.vo.JsonRequest;
import com.cqtczh.qhd.driver.vo.JsonResponse;
import com.cqtczh.qhd.driver.vo.State;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;

/**
 * * @author 作者 陈修园:
 * 
 * @date 创建时间：2015-5-5 上午10:33:05
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
@SuppressWarnings("deprecation")
public class ProjectHomeActivity extends TabActivity {

	private TabHost tabHost;
	private RadioGroup radioGroup;
	private RelativeLayout botton_Layout;
	int startLeft = 0;
	int imageviewwieth = 0;
	AppContent app;
	RadioButton radio_two, radio_three, radio_four; // , radio_five;
	List<RadioButton> view = null;
	List<Integer> imagepath;

	/**
	 * 侧面菜单类容
	 */
	private TextView menu_Name;
	private View menu_integral, menu_place, account, shop, change, update,
			serve, introduce, deal, menu_esc, menu_night;

	/**
	 * 
	 */
	private ImageView head_left;
	private Context context;

	private SlidingMenu menu;

	private PreferencesCookieStore preferencesCookieStore;

	@SuppressWarnings({ "deprecation", "null" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.project_home_activity);
		L.v("ProjectHomeActivity", "ProjectHomeActivity");
		app = (AppContent) getApplication();
		botton_Layout = (RelativeLayout) findViewById(R.id.layout_bottom);
		JPushInterface.resumePush(getApplicationContext());
		tabHost = getTabHost();
		tabHost.addTab(tabHost.newTabSpec("home").setIndicator("Home")
				.setContent(new Intent(this, new_home_activity.class)));
		tabHost.addTab(tabHost.newTabSpec("two").setIndicator("two")
				.setContent(new Intent(this, OrdersActivity.class))); // Home_Info
																		// //OrdersActivity
		tabHost.addTab(tabHost.newTabSpec("three").setIndicator("three")
				.setContent(new Intent(this, CarTeamConter.class)));
		// tabHost.addTab(tabHost.newTabSpec("four").setIndicator("four")
		// .setContent(new Intent(this, SettingsActivity.class)));
		radioGroup = (RadioGroup) findViewById(R.id.main_radio);
		radioGroup.setOnCheckedChangeListener(checkedChangeListener);
		radio_two = (RadioButton) findViewById(R.id.radio_two);
		radio_three = (RadioButton) findViewById(R.id.radio_three);
		radio_four = (RadioButton) findViewById(R.id.radio_four);
		// radio_five = (RadioButton) findViewById(R.id.radio_five);
		radio_two.setBackgroundResource(R.drawable.home);
		view = new ArrayList<RadioButton>();
		view.add(radio_two);
		view.add(radio_three);
		view.add(radio_four);
		// view.add(radio_five);
		imagepath = new ArrayList<Integer>();
		imagepath.add(R.drawable.background_home);
		imagepath.add(R.drawable.background_order);
		imagepath.add(R.drawable.background_car);
		// imagepath.add(R.drawable.background_user);
		OnListenerPushMessage();
		// GetRequestMessage();
		OnSetCarDeletMessage();
		OnSetComplete();
		/*
		 * 设置司机验证消息的监听
		 */
		driverVerificationMessage();
		setMenu();
		setUserInfoCheckComplete();
		app.upBaiDuPussh();
		checkVersion(1);
		// try {
		// CheckVersion();
		// } catch (NameNotFoundException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		checkLogAndUpload();
//		checkBaiduPushEable();
	}

	/**
	 * 
	 * @Title: setMenu
	 * @Description: TODO(APP的左侧面菜单)
	 * @param 参数说明
	 * @return void 返回类型
	 * @throws
	 */
	private void setMenu() {

		context = this;
		menu = new SlidingMenu(this);

		CEMenu menuOnClick = new CEMenu();
		menu.setMode(SlidingMenu.LEFT);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		View view = LayoutInflater.from(this).inflate(R.layout.layout_menu,
				null);
		menu.setMenu(view);
		introduce = (View) view.findViewById(R.id.introduce);
		menu_integral = (View) view.findViewById(R.id.menu_integral);
		menu_Name = (TextView) view.findViewById(R.id.menu_Name);
		if (app.userInfoBeen == null) {
			Intent intent = new Intent(this, UserLoginActivity.class);
			startActivity(intent);
			this.finish();
			return;
		} else {

			String name = app.userInfoBeen.getCarownername();
			if (name != null && name.length() > 0) {
				menu_Name.setText(app.userInfoBeen.getCarownername());
			} else {
				menu_Name.setText("");
			}
		}
		ImageView head = (ImageView) view.findViewById(R.id.portrait);
		BitmapUtils utils = new BitmapUtils(this);
		if (app.userInfoBeen.getHeadportrait() != null
				&& app.userInfoBeen.getHeadportrait().length() > 0) {
			utils.display(head,
					GagApi.IMAGE_PAHT_HOST + app.userInfoBeen.getHeadportrait());
		}

		// 我的账户
		account = (View) view.findViewById(R.id.account);
		// 积分商城
		shop = (View) view.findViewById(R.id.shop);
		// 密码修改
		change = (View) view.findViewById(R.id.change);
		// 检查更新
		update = (View) view.findViewById(R.id.update);
		// 运输服务
		serve = (View) view.findViewById(R.id.serve);
		// Q货的介绍
		introduce = (View) view.findViewById(R.id.introduce);
		// 用户协议
		deal = (View) view.findViewById(R.id.deal);
		//
		// deal = (TextView) view.findViewById(R.id.deal);
		// 夜间模式
//		menu_night = view.findViewById(R.id.menu_night);
		// 注销退出
		menu_esc = view.findViewById(R.id.menu_esc);

		// head_title = (TextView) findViewById(R.id.head_title);
		// head_title.setText("首页");
		menu_integral.setOnClickListener(menuOnClick);
		menu_Name.setOnClickListener(menuOnClick);
		// menu_place.setOnClickListener(new CEMenu());
		account.setOnClickListener(menuOnClick);
		shop.setOnClickListener(menuOnClick);
		change.setOnClickListener(menuOnClick);
		update.setOnClickListener(menuOnClick);
		serve.setOnClickListener(menuOnClick);
		menu_esc.setOnClickListener(menuOnClick);
//		menu_night.setOnClickListener(menuOnClick);
		deal.setOnClickListener(menuOnClick);
		introduce.setOnClickListener(menuOnClick);
		app.setMenuLiseter(new menuCEHD() {

			@Override
			public void showMenu() {
				// TODO Auto-generated method stub

				// menu.showContent();
				menu.showMenu();
				String name = app.userInfoBeen.getCarownername();
				if (name != null && name.length() > 0) {
					menu_Name.setText(app.userInfoBeen.getCarownername());
				} else {
					menu_Name.setText("");
				}
			}

			@Override
			public void enabelTouchShowMenu() {
				// TODO Auto-generated method stub
			//	L.v("启动", "启动");
				menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
			}

			@Override
			public void disableTouchShowMenu() {
				// TODO Auto-generated method stub
			//	L.v("停止", "停止");
				menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
			}

		});
		// head_left.setOnClickListener(this);
	
	}

	/**
	 * 
	 * @Title: OnSetComplete
	 * @Description: TODO(公司的车队邀请)
	 * @param 参数说明
	 * @return void 返回类型
	 * @throws
	 */
	private void OnSetComplete() {
		app.SetCompleteCarTeamMessage(new CompleteCarTeam() {

			@Override
			public void GetCompleteCarTeam(String msg, final String CarTeamID,
					String CompleteName) {
				// TODO Auto-generated method stub
				Toast.makeText(ProjectHomeActivity.this, msg, Toast.LENGTH_LONG)
						.show();

				// 创建退出对话框
				LayoutInflater inflater = LayoutInflater
						.from(ProjectHomeActivity.this);
				LinearLayout layout = (LinearLayout) inflater.inflate(
						R.layout.alert_dialog, null);
				final Dialog isExit = new AlertDialog.Builder(
						ProjectHomeActivity.this).create();
				isExit.show();
				isExit.setContentView(layout);
				// 设置对话框标题
				TextView title = (TextView) layout.findViewById(R.id.title);
				TextView message = (TextView) layout.findViewById(R.id.message);
				title.setText("提醒");
				// 设置对话框消息
				message.setText(msg);
				Button check = (Button) layout.findViewById(R.id.check);
				Button giveup = (Button) layout.findViewById(R.id.giveup);
				check.setText("查看");
				giveup.setText("忽略这一次");
				check.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(ProjectHomeActivity.this,
								Car_Team_Info.class);
						// 车队ID
						intent.putExtra("CompleteCarTeamInfo", CarTeamID);
						startActivity(intent);
						isExit.dismiss();
					}
				});
				giveup.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						isExit.dismiss();
					}
				});

			}
		});
	}

	private void OnSetCarDeletMessage() {
		app.SetGetCarTeamDelete(new GetCarTeamDelete() {

			@Override
			public void GetDeleteMessage(String msg, String CarTeamLeader,
					String CarTeamId) {
				// TODO Auto-generated method stub
				Toast.makeText(ProjectHomeActivity.this, msg, Toast.LENGTH_LONG)
						.show();

				// 创建退出对话框
				LayoutInflater inflater = LayoutInflater
						.from(ProjectHomeActivity.this);
				LinearLayout layout = (LinearLayout) inflater.inflate(
						R.layout.alert_dialog, null);
				final Dialog isExit = new AlertDialog.Builder(
						ProjectHomeActivity.this).create();
				isExit.show();

				isExit.setContentView(layout);
				// 设置对话框标题
				layout.setGravity(Gravity.CENTER);

				TextView title = (TextView) layout.findViewById(R.id.title);
				TextView message = (TextView) layout.findViewById(R.id.message);

				title.setText("提醒");
				// 设置对话框消息
				message.setText(msg);
				Button check = (Button) layout.findViewById(R.id.check);
				Button giveup = (Button) layout.findViewById(R.id.giveup);
				// layout.removeView(giveup);
				// layout.setGravity(Gravity.CENTER);
				giveup.setVisibility(View.GONE);
				check.setText("确定");
				giveup.setText("忽略这一次");
				check.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						isExit.dismiss();
					}
				});
				// giveup.setOnClickListener(new OnClickListener() {
				//
				// @Override
				// public void onClick(View arg0) {
				// // TODO Auto-generated method stub
				// isExit.dismiss();
				// }
				// });
			}

		});
	}

	private void OnListenerPushMessage() {
		app.setCarTeamMessageLister(new getCarTeamMessage() {

			@Override
			public void carTeamMessage(String msg, final String CarTeamLeader,
					final String CarTeamId) {
				// TODO Auto-generated method stub
				Toast.makeText(ProjectHomeActivity.this, msg, Toast.LENGTH_LONG)
						.show();

				// 创建退出对话框
				LayoutInflater inflater = LayoutInflater
						.from(ProjectHomeActivity.this);
				LinearLayout layout = (LinearLayout) inflater.inflate(
						R.layout.alert_dialog, null);
				final Dialog isExit = new AlertDialog.Builder(
						ProjectHomeActivity.this).create();
				isExit.show();

				isExit.setContentView(layout);
				// 设置对话框标题
				TextView title = (TextView) layout.findViewById(R.id.title);
				TextView message = (TextView) layout.findViewById(R.id.message);
				title.setText("提醒");
				// 设置对话框消息
				message.setText("你有车队邀请信息请查看");
				Button check = (Button) layout.findViewById(R.id.check);
				Button giveup = (Button) layout.findViewById(R.id.giveup);
				check.setText("查看");
				giveup.setText("忽略这一次");
				check.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(ProjectHomeActivity.this,
								CarTeamRequestInfo.class);
						// 车队ID
						intent.putExtra("CarTeamId", CarTeamId);
						// 车队队长ID
						intent.putExtra("CarTeamLeadr", CarTeamLeader);
						startActivity(intent);
						isExit.dismiss();
					}
				});
				giveup.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						isExit.dismiss();
					}
				});
			}
		});
	}

	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {
		int i = 0;

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.radio_two:
				if (i != 0) {
					view.get(i).setBackgroundResource(imagepath.get(i));
					tabHost.setCurrentTabByTag("home");
					radio_two.setBackgroundResource(R.drawable.home);
					i = 0;
				}
				break;
			case R.id.radio_three:
				if (i != 1) {
					view.get(i).setBackgroundResource(imagepath.get(i));
					tabHost.setCurrentTabByTag("two");
					radio_three.setBackgroundResource(R.drawable.order);
					i = 1;
				}
				break;
			case R.id.radio_four:
				if (i != 2) {
					view.get(i).setBackgroundResource(imagepath.get(i));
					tabHost.setCurrentTabByTag("three");
					radio_four.setBackgroundResource(R.drawable.car);
					i = 2;
				}
				break;
			// case R.id.radio_five:
			// if (i != 3) {
			// view.get(i).setBackgroundResource(imagepath.get(i));
			// tabHost.setCurrentTabByTag("four");
			// radio_five.setBackgroundResource(R.drawable.user);
			// i = 3;
			// }
			// break;
			default:
				break;
			}
		}
	};

	/**
	 * 
	 * @Title: driverVerificationMessage
	 * @Description: TODO(司机验证成功信息)
	 * @param 参数说明
	 * @return void 返回类型
	 * @throws
	 */
	public void driverVerificationMessage() {
		app.setdriverVerificationMessage(new com.cqtczh.qhd.driver.AppContent.driverVerificationMessage() {

			@Override
			public void verification(final String message) {
				// TODO Auto-generated method stub

				// 创建退出对话框
				LayoutInflater inflater = LayoutInflater
						.from(ProjectHomeActivity.this);
				LinearLayout layout = (LinearLayout) inflater.inflate(
						R.layout.alert_dialog, null);
				final Dialog isExit = new AlertDialog.Builder(
						ProjectHomeActivity.this).create();
				isExit.show();
				isExit.setContentView(layout);
				// 设置对话框标题
				TextView title = (TextView) layout.findViewById(R.id.title);
				TextView messageview = (TextView) layout
						.findViewById(R.id.message);
				title.setText("提醒");
				// 设置对话框消息
				messageview.setText(message);
				Button check = (Button) layout.findViewById(R.id.check);
				Button giveup = (Button) layout.findViewById(R.id.giveup);
				check.setText("查看");
				giveup.setText("知道了");
				check.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(ProjectHomeActivity.this,
								MessageActivity.class);
						intent.putExtra("message", message);

					}
				});
				giveup.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						isExit.dismiss();
					}
				});
			}
		});
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			/*if (event.getAction() == KeyEvent.ACTION_DOWN
					&& event.getRepeatCount() == 0) {
				LayoutInflater inflater = LayoutInflater
						.from(ProjectHomeActivity.this);
				LinearLayout layout = (LinearLayout) inflater.inflate(
						R.layout.alert_dialog, null);
				final Dialog isExit = new AlertDialog.Builder(
						ProjectHomeActivity.this).create();
				isExit.show();
				isExit.setContentView(layout);
				// 设置对话框标题
				TextView title = (TextView) layout.findViewById(R.id.title);
				TextView message = (TextView) layout.findViewById(R.id.message);
				title.setText("提醒");
				// 设置对话框消息
				message.setText("是否要退出Q货的");
				Button check = (Button) layout.findViewById(R.id.check);
				Button giveup = (Button) layout.findViewById(R.id.giveup);
				check.setText("确定");
				giveup.setText("取消退出");
				check.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						isExit.dismiss();
						LocationClient mLocationClient = app.mLocationClient;
						mLocationClient.stop();
						app.DriverIsOnline = false;
						app.add(app.latitude, app.longitude, app.adrr, "off",
								app.messagepushuserId, app.channelId);
					//	PushManager.stopWork(getApplicationContext());
					//	JPushInterface.stopPush(ProjectHomeActivity.this.getApplicationContext());
						ProjectHomeActivity.this.finish();
						AppManager.getAppManager().finishActivity(UserLoginActivity.class);
					}
				});
				giveup.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						isExit.dismiss();
					}
				});
			}*/
			moveTaskToBack(false);
			return true;
		}
		return false;

	};

	/**
	 * 
	 * @Title: setUserInfoCheckComplete
	 * @Description: TODO(建立司机信息完善接口)
	 * @param 参数说明
	 * @return void 返回类型
	 * @throws
	 */
	private void setUserInfoCheckComplete() {
		app.setUserInfoCheck(new userInfoCheck() {

			@Override
			public void getUserInfoCheck(String messages) {
				// TODO Auto-generated method stub
				app.AntLogin();
				LayoutInflater inflater = LayoutInflater
						.from(ProjectHomeActivity.this);
				LinearLayout layout = (LinearLayout) inflater.inflate(
						R.layout.alert_dialog, null);
				final Dialog isExit = new AlertDialog.Builder(
						ProjectHomeActivity.this).create();
				isExit.show();
				isExit.setContentView(layout);
				// 设置对话框标题
				TextView title = (TextView) layout.findViewById(R.id.title);
				TextView message = (TextView) layout.findViewById(R.id.message);
				title.setText("提醒");
				// 设置对话框消息
				message.setText(messages);
				Button check = (Button) layout.findViewById(R.id.check);
				Button giveup = (Button) layout.findViewById(R.id.giveup);
				giveup.setVisibility(View.GONE);
				check.setText("确定");
				// giveup.setText("取消");
				check.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub\
						// app.AntLogin();
						isExit.dismiss();
					}
				});

				// giveup.setOnClickListener(new OnClickListener() {
				//
				// @Override
				// public void onClick(View arg0) {
				// // TODO Auto-generated method stub
				// isExit.dismiss();
				//
				// }
				// });

			}
		});
	}

	/** 监听对话框里面的button点击事件 */
	DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
				finish();
				break;
			case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onDestroy() {

		super.onDestroy();
	};

	/*
	 * 左边的菜单的点击事件
	 */
	public class CEMenu implements OnClickListener {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.account:
				L.v("出来", "出来");
				Intent intent = new Intent(ProjectHomeActivity.this,
						MessageActivity.class);
				startActivity(intent);
				break;
			case R.id.update:
					checkVersion(2);

				break;

			case R.id.serve:
				Intent serveInent = new Intent();
				serveInent.setClass(ProjectHomeActivity.this,
						AboutUsActivity.class);
				serveInent.putExtra("mark", "运输服务条款");
				startActivity(serveInent);
				break;
			case R.id.deal:
				Intent dealIntent = new Intent();
				dealIntent.setClass(ProjectHomeActivity.this,
						AboutUsActivity.class);
				dealIntent.putExtra("mark", "用户协议");
				startActivity(dealIntent);
				break;

			case R.id.introduce:
				Intent introduceIntent = new Intent();
				introduceIntent.setClass(ProjectHomeActivity.this,
						AboutUsActivity.class);
				introduceIntent.putExtra("mark", "Q货的介绍");
				startActivity(introduceIntent);
				break;

//			case R.id.menu_night:
//				Toast.makeText(ProjectHomeActivity.this, "该功能即将上线，尽请期待。",
//						Toast.LENGTH_LONG).show();

//				break;
			case R.id.shop:
				Toast.makeText(ProjectHomeActivity.this, "该功能即将上线，尽请期待。",
						Toast.LENGTH_LONG).show();

				break;
			case R.id.menu_esc:
				exitCEHD();
				break;
			default:
				break;
			}
		}

	}

	/**
	 * 
	* @Title: checkVersion 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param type  是否有toast  1没有 2有
	* @return void    返回类型 
	* @throws
	 */
	private void checkVersion(final int type) {
		tools.CheckVersion.check(this, new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					// 创建退出对话框
					LayoutInflater inflater = LayoutInflater
							.from(ProjectHomeActivity.this);
					LinearLayout layout = (LinearLayout) inflater.inflate(
							R.layout.alert_dialog, null);
					final Dialog isExit = new AlertDialog.Builder(
							ProjectHomeActivity.this).create();
					isExit.show();
					isExit.setContentView(layout);
					// 设置对话框标题
					TextView title = (TextView) layout.findViewById(R.id.title);
					TextView message = (TextView) layout
							.findViewById(R.id.message);
					title.setText("消息提醒");
					// 设置对话框消息
					message.setText("有新的版本更新");
					Button check = (Button) layout.findViewById(R.id.check);
					Button giveup = (Button) layout.findViewById(R.id.giveup);
					check.setText("下载");
					giveup.setText("忽略");
					check.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							Intent intents = new Intent();
							intents.setClass(ProjectHomeActivity.this,
									DownLoadAPP.class);
							String root = SD_Finder.GetPaht();
							if (root != null && root.length() > 0) {
								intents.putExtra("path", root);
								startService(intents);
								isExit.dismiss();
							} else {
								isExit.dismiss();
								Toast.makeText(ProjectHomeActivity.this,
										"未找到SD卡，无法下载", Toast.LENGTH_LONG)
										.show();
							}
						}
					});
					giveup.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							isExit.dismiss();
						}
					});
				}else {
					if (type==2) {
						Toast.makeText(ProjectHomeActivity.this, "已经是最新版本", Toast.LENGTH_LONG).show();
					}
				}
			}
		});

	}

	/**
	 * 
	 * @Title: CheckVersion
	 * @Description: TODO(APP检查版本)
	 * @param @throws NameNotFoundException 参数说明
	 * @return void 返回类型
	 * @throws
	 */
//	private void CheckVersion() throws NameNotFoundException {
//		// int verCode =
//		// this.getPackageManager().getPackageInfo(getPackageName(),
//		// 0).versionCode;
//		// System.out.println("版本号：" + verCode);
//		try {
//			int verCode = this.getPackageManager().getPackageInfo(
//					getPackageName(), 0).versionCode;
//			Map<String, Object> dataMap = new HashMap<String, Object>();
//			// JsonRequest jr = JsonRequest.getInit();
//			dataMap.put("versionNumber", verCode + "");
//			dataMap.put("versionType", "司机版本");
//			// jr.setData(dataMap);
//			final Gson gson = new Gson();
//			String json = gson.toJson(dataMap);
//			// System.out.println(json);
//			HttpUtils http = new HttpUtils();
//			http.configCookieStore(AppContent.getPreferencesCookieStore());
//			RequestParams params = new RequestParams();
//			params.addBodyParameter("jsonParam", json);
//			http.send(HttpRequest.HttpMethod.POST, GagApi.CheckVersion, params,
//					new RequestCallBack<String>() {
//
//						@Override
//						public void onSuccess(ResponseInfo<String> responseInfo) {
//
//							L.v("responseInfo", responseInfo.result);
//							if ("200".equals(State.SUCEECCODE)) {
//								// L.v("去更新2","去更新");
//								if (GagApi.UPAPKPATH != null) {
//
//									// 创建退出对话框
//									LayoutInflater inflater = LayoutInflater
//											.from(ProjectHomeActivity.this);
//									LinearLayout layout = (LinearLayout) inflater
//											.inflate(R.layout.alert_dialog,
//													null);
//									final Dialog isExit = new AlertDialog.Builder(
//											ProjectHomeActivity.this).create();
//									isExit.show();
//									isExit.setContentView(layout);
//									// 设置对话框标题
//									TextView title = (TextView) layout
//											.findViewById(R.id.title);
//									TextView message = (TextView) layout
//											.findViewById(R.id.message);
//									title.setText("消息提醒");
//									// 设置对话框消息
//									message.setText("有新的版本更新");
//									Button check = (Button) layout
//											.findViewById(R.id.check);
//									Button giveup = (Button) layout
//											.findViewById(R.id.giveup);
//									check.setText("下载");
//									giveup.setText("忽略");
//									check.setOnClickListener(new OnClickListener() {
//
//										@Override
//										public void onClick(View arg0) {
//											// TODO Auto-generated method stub
//											Intent intents = new Intent();
//											intents.setClass(
//													ProjectHomeActivity.this,
//													DownLoadAPP.class);
//											String root = SD_Finder.GetPaht();
//											if (root != null
//													&& root.length() > 0) {
//												intents.putExtra("path", root);
//												startService(intents);
//												isExit.dismiss();
//											} else {
//												isExit.dismiss();
//												Toast.makeText(
//														ProjectHomeActivity.this,
//														"未找到SD卡，无法下载",
//														Toast.LENGTH_LONG)
//														.show();
//											}
//										}
//									});
//									giveup.setOnClickListener(new OnClickListener() {
//
//										@Override
//										public void onClick(View arg0) {
//											// TODO Auto-generated method stub
//											isExit.dismiss();
//										}
//									});
//
//									// UpdateManager uManager = new
//									// UpdateManager(
//									// new_home_activity.this);
//									// uManager.apkUrl = GagApi.UPAPKPATH;
//									// uManager.checkUpdateInfo();
//								} else {
//									System.out.println("已经是最新的版本");
//									//
//								}
//								// findXq();
//
//							} else {
//								// System.out.println(state.getMsg());
//								// ToastUtils.showLong(state.getMsg());
//								Toast.makeText(ProjectHomeActivity.this,
//										"已经是最新的版本", Toast.LENGTH_LONG).show();
//							}
//						}
//
//						public void onFailure(HttpException error, String msg) {
//							// System.out.println(msg);
//						}
//					});
//		} catch (NameNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

	private void exitCEHD() {
		LocationClient mLocationClient = app.mLocationClient;
		mLocationClient.stop();
		app.DriverIsOnline = false;
		app.add(app.latitude, app.longitude, app.adrr, "off",
				app.messagepushuserId, app.channelId);
//		PushManager.stopWork(getApplicationContext());
		JPushInterface.stopPush(getApplicationContext());
		SPUtils.remove(ProjectHomeActivity.this, "driverMobile");
		SPUtils.remove(ProjectHomeActivity.this, "driverPassword");
		SPUtils.clear(ProjectHomeActivity.this);
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.cancel(10);
		int currentVersion = android.os.Build.VERSION.SDK_INT;
		if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
			Intent startMain = new Intent(Intent.ACTION_MAIN);
			startMain.addCategory(Intent.CATEGORY_HOME);
			startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(startMain);
			System.exit(0);
		} else {// android2.1
			ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
			am.restartPackage(getPackageName());
		}

	}

	/**
	 * 
	 * @Title: checkLogAndUpload
	 * @Description: TODO(检查log并且上传)
	 * @param 参数说明
	 * @return void 返回类型
	 * @throws
	 */
	private void checkLogAndUpload() {
		String rootpath = SD_Finder.GetPaht();
		if (rootpath != null && rootpath.length() > 0) {
			String path = rootpath + GagApi.TCZH_File;
			File folder = new File(path);
			List<String> allLogfile = new ArrayList<String>();
			LogFileFind filter = new LogFileFind("log");
			String[] logfilenames = folder.list(filter);
			if (logfilenames != null && logfilenames.length > 0) {

				allLogfile.addAll(Arrays.asList(logfilenames));
				// 计算有多少个文件
				// int fileSum=allLogfile.size();
				for (int i = 0; i < allLogfile.size(); i++) {
					upLogFIleUpload(path + "/" + allLogfile.get(i));
				}
			}
		}

	}

	private void upLogFIleUpload(final String path) {
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		httpJsonTool.setLogHandle(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					File file = new File(path);
					file.delete();
				}
			}
		});
		httpJsonTool.UpLoadFile(path);
	}

	/**
	 * 检查百度推送是否开启
	 */
//	private void checkBaiduPushEable(){
//		if (!(PushManager.isPushEnabled(getApplicationContext()))) {
//			PushManager.startWork(getApplicationContext(),
//					PushConstants.LOGIN_TYPE_API_KEY, "xGVLGuGeyehuEoNSxwRxFeEu");
//
//		}else {
//			PushManager.startWork(getApplicationContext(),
//					PushConstants.LOGIN_TYPE_API_KEY, "xGVLGuGeyehuEoNSxwRxFeEu");
//		}		
//	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		checkBaiduPushEable();
	}
	
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
//		checkBaiduPushEable();
	}
	
	// ONSAVEINSTANCESTATE

	@Override
	protected void onRestoreInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(outState);
		LocationClient mLocationClient = app.mLocationClient;
		mLocationClient.stop();
		app.DriverIsOnline = false;
		
		app.add(app.latitude, app.longitude, app.adrr, "off",
				app.messagepushuserId, app.channelId);
		L.e(app.latitude+"", app.longitude+"");
//		PushManager.stopWork(getApplicationContext());
	}
	
	

}
